/**
 * author:Administrator
 * date:2013-5-21
 */
package com.roc.web.action.merinfo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionContext;
import com.roc.bean.MerinfoSearchBean;
import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.Operators;
import com.roc.pojo.OrgBean;
import com.roc.service.IMerchantServices;
import com.roc.service.IPagenitionService;
import com.roc.service.IorgService;
import com.roc.service.ServiceException;
import com.roc.util.Constance;
import com.roc.util.constant.ConfigProperties;
import com.roc.util.constant.Constant;
import com.roc.util.constant.MerchantStatus;
import com.roc.util.constant.Role;

/**
 * @author Administrator
 * 商户查询 
 * 批量商户补录 
 *
 */
@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Namespace("/merinfo")
public class MerchantAction extends PaginationBaseAction {
	

	private static final long serialVersionUID = 5236170890634487270L;
	
	private Logger logger=LogManager.getLogger(MerchantAction.class);
	
	private Operators merchant;
	private List<OrgBean> orgList=new ArrayList<OrgBean>();
	private String[] agentList ;
	private MerchantSearchCondition searchCondition;

	private List<Object> profitRsList=new ArrayList<Object>();
	
	
	
	@Autowired 
	IMerchantServices merchantServices;
	@Autowired
	IPagenitionService pagenitionService;
	
	@Autowired
	IorgService orgService;
	
	@Action(value = "qrtInit", results = { @Result(name = "success", location = "/pages/merinfo/merinfoList.jsp"),
            @Result(name="error",location="/pages/merinfo/merinfoList.jsp"),
            @Result(name="login",location="/pages/redirect.jsp") })

	public String qrtInit() throws Exception{
		try{
			init();
		}catch (Exception e) {
			// TODO: handle exception
			logger.error(e.getMessage());
		}
		return SUCCESS;
	}
	
	@Action(value = "qrtImportInit", results = { @Result(name = "success", location = "/pages/merinfo/merinfoImportList.jsp"),
            @Result(name="error",location="/pages/merinfo/merinfoImportList.jsp"),
            @Result(name="login",location="/pages/redirect.jsp") })

            /**
             * 补录查询初始化
             */
	public String qrtImportInit() throws Exception{
		try{
			init();
		}catch (Exception e) {
			// TODO: handle exception
			logger.error(e.getMessage());
		}
		return SUCCESS;
	}

	@Action(value = "search", results = { @Result(name = "success", location = "/pages/merinfo/merinfoList.jsp"),
           // @Result(name="error",location="/pages/merinfoList.jsp"),
            @Result(name="login",location="/pages/merinfo/merinfoImportList.jsp") })

	public String search() throws Exception{
		Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		
		if(searchCondition==null){
			searchCondition=(MerchantSearchCondition)getSearchConditionFromSession();
			if(searchCondition==null){
				searchCondition=new MerchantSearchCondition();
			}
		}
		String midStatus = searchCondition.getMidStatus();
		try {
			
			init();
			
			if(Role.AGENT.getCode().equals(op.getRoleType())){//代理商
				 List<Operators> opList=merchantServices.listAllSubMerchants(op.getId());
				 agentList= new String[opList.size()];
				 for (int i=0;i<opList.size();i++) {
					Operators o=opList.get(i);
					agentList[i]=Long.toString(o.getId());
				}
			   searchCondition.setAgentId(agentList);
			}else if(Role.AGENT_BUSS.getCode().equals(op.getRoleType())){//业务员
				searchCondition.setQueryOpr(op.getAccountNo());
			}else if(Role.MERCHANT.getCode().equals(op.getRoleType())){//商户
				searchCondition.setMerchantNo(op.getAccountNo());
			}
			
			//this.setCurrentPage(this.checkCurrentPage());
			int rows=pagenitionService.countSQL(searchCondition);
			searchRsList=pagenitionService.findAllSQL(searchCondition, calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);
			List<MerinfoSearchBean> msList=new ArrayList<MerinfoSearchBean>();
			if(searchRsList.size()>0 && searchRsList!=null){
				
				Iterator iterator=searchRsList.iterator();
				while (iterator.hasNext()) {
					Object[] obj= (Object[])iterator.next();
					MerinfoSearchBean  msb=new MerinfoSearchBean(); 
					msb.setMid(Constance.Null2EmptyTrim(obj[0]));
					msb.setMerName(Constance.Null2EmptyTrim(obj[1]));
					msb.setMerBrName(Constance.Null2EmptyTrim(obj[2]));
					msb.setMerKind(Constance.Null2EmptyTrim(obj[3]));
					msb.setFlag(MerchantStatus.valueOfCode(Integer.parseInt(obj[4].toString())).getName());
					msb.setRsvl(Constance.Null2EmptyTrim(obj[5]));
					msb.setRsvl3(Constance.Null2EmptyTrim(obj[6]));
					msb.setInterContact(Constance.Null2EmptyTrim(obj[7]));
					msb.setPerPhone(Constance.Null2EmptyTrim(obj[8]));
					msb.setPerName(Constance.Null2EmptyTrim(obj[9]));
					msb.setCycleId(Constance.Null2EmptyTrim(obj[10]));
					msb.setCatdType(Constance.Null2EmptyTrim(obj[11]));
					msb.setCardNo(Constance.Null2EmptyTrim(obj[12]));
					msb.setAddr(Constance.Null2EmptyTrim(obj[13]));
					msb.setAreaCode(Constance.Null2EmptyTrim(obj[14]));
					msb.setAgentId(Constance.Null2EmptyTrim(obj[15]));
					msb.setFlagCode(Constance.Null2EmptyTrim(obj[4].toString()));
					msb.setRate(Constance.Null2EmptyTrim(obj[16]));
					msb.setRateType(Constance.Null2EmptyTrim(obj[17]));
					msb.setAmt(Constance.Null2EmptyTrim(obj[18]));
					msb.setBus_lic(Constance.Null2EmptyTrim(obj[19]));
					msb.setRealName(Constance.Null2EmptyTrim(obj[20]));
					msList.add(msb);
				}
				
				
			}
			
			this.setTotalPage(this.calcuTotalPage(rows));
			getRequest().setAttribute(SEARCH_RS_LIST, msList);
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			setSearchToConditionSession(searchCondition);
			
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			addActionError(e.getMessage());
			return ERROR;
		}
		if("12".equals(midStatus)){
			return LOGIN;
		}
		return SUCCESS;
	}
	//初始化
	public void init() throws Exception{
		Operators loginOperators = (Operators) ActionContext.getContext().getSession().get(Constant.SESSION_LOGIN_MERCHANT);
		try {
			//初始化地区
			getRequest().setAttribute("areaList", ConfigProperties.getAreaCodeProperties());
			//初始化机构
			orgList=orgService.listOrgs(loginOperators);
			//初始化结算周期
			List<Object> objList=merchantServices.queryCycle();
			getRequest().setAttribute("cycList", objList);
			Map<String, String > statusMap=new TreeMap<String, String>();
			statusMap.put(Long.toString(MerchantStatus.S_0.getCode()),MerchantStatus.S_0.getName());
			statusMap.put(Long.toString(MerchantStatus.S_1.getCode()),MerchantStatus.S_1.getName());
			statusMap.put(Long.toString(MerchantStatus.S_2.getCode()),MerchantStatus.S_2.getName());
			statusMap.put(Long.toString(MerchantStatus.S_3.getCode()),MerchantStatus.S_3.getName());
			statusMap.put(Long.toString(MerchantStatus.S_4.getCode()),MerchantStatus.S_4.getName());
			statusMap.put(Long.toString(MerchantStatus.S_5.getCode()),MerchantStatus.S_5.getName());
			statusMap.put(Long.toString(MerchantStatus.S_6.getCode()),MerchantStatus.S_6.getName());
			statusMap.put(Long.toString(MerchantStatus.S_7.getCode()),MerchantStatus.S_7.getName());
			statusMap.put(Long.toString(MerchantStatus.S_10.getCode()),MerchantStatus.S_10.getName());
			statusMap.put(Long.toString(MerchantStatus.S_11.getCode()),MerchantStatus.S_11.getName());
			statusMap.put(Long.toString(MerchantStatus.S_13.getCode()),MerchantStatus.S_13.getName());
			statusMap.put(Long.toString(MerchantStatus.S_14.getCode()),MerchantStatus.S_14.getName());
			statusMap.put(Long.toString(MerchantStatus.S_15.getCode()),MerchantStatus.S_15.getName());
			getRequest().setAttribute("statusList", statusMap);
			
		} catch (Exception e) {
			logger.error("商户查询初始化失败",e);
			throw new Exception("商户查询初始化失败");
		}
	}
	public static void main(String[] args) {
		MerchantStatus.valueOfCode(0);
		System.out.println(MerchantStatus.valueOfCode(0).getName());
	}
	
	public void initSomeDataForAction() {
		
	}
	@Override
	protected void dealWithTheListSpecially(List<Object> list) {
		// TODO Auto-generated method stub
		
	}
	private String[] dealSomeData(List<Operators> list){
		String[] str=null;
		if(list!=null&&list.size()>0){
			str=new String[list.size()];
			for(int i=0;i<list.size();i++){
				str[i]=list.get(i).getAccountNo();
			}
		}
		return str;
	}
	/* (non-Javadoc)
	 * @see com.roc.page.PaginationBaseAction#getInitPageSize()
	 */
	@Override
	protected String getInitPageSize() {
		// TODO Auto-generated method stub
		return Constance.PAGE_MAX_SIZE+"";
	}

	/* (non-Javadoc)
	 * @see com.roc.page.PaginationBaseAction#getSearchConditionFromSession()
	 */
	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		// TODO Auto-generated method stub
		return (MerchantSearchCondition)getSession().getAttribute(SEARCH_SESSION_BEAN_MERCHANT_QRT);
	}

	/* (non-Javadoc)
	 * @see com.roc.page.PaginationBaseAction#getSearchConditionFromSubClass()
	 */
	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {
		// TODO Auto-generated method stub
		return searchCondition;
	}

	/* (non-Javadoc)
	 * @see com.roc.page.PaginationBaseAction#setSearchConditionToSubClass(com.roc.page.BaseSearchCondition)
	 */
	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {
		this.setSearchConditionToSubClass(searchCondition);
	}

	/* (non-Javadoc)
	 * @see com.roc.page.PaginationBaseAction#setSearchToConditionSession(com.roc.page.BaseSearchCondition)
	 */
	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {
		getSession().setAttribute(SEARCH_SESSION_BEAN_MERCHANT_QRT, searchCondition);
	}
	/**
	 * @return the searchCondition
	 */
	public MerchantSearchCondition getSearchCondition() {
		
		return searchCondition;
	}
	/**
	 * @param searchCondition the searchCondition to set
	 */
	public void setSearchCondition(MerchantSearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}
	
	/**
	 * @return the merchant
	 */
	public Operators getMerchant() {
		return merchant;
	}
	/**
	 * @param merchant the merchant to set
	 */
	public void setMerchant(Operators merchant) {
		this.merchant = merchant;
	}


	

	/**
	 * @return the profitRsList1
	 */
	public List<Object> getProfitRsList() {
		return profitRsList;
	}


	/**
	 * @param profitRsList1 the profitRsList1 to set
	 */
	public void setProfitRsList1(List<Object> profitRsList) {
		this.profitRsList = profitRsList;
	}

	public List<OrgBean> getOrgList() {
		return orgList;
	}

	public void setOrgList(List<OrgBean> orgList) {
		this.orgList = orgList;
	}

	


	
	

}
